#include "time.h"

#include "ntstrsafe.h"

using namespace windows::kernal;

NTSTATUS Time::GetCurrentTimeFields(PTIME_FIELDS time) {
  LARGE_INTEGER system_time, local_time;
  if (!time) {
    return STATUS_INVALID_PARAMETER_1;
  }
  KeQuerySystemTime(&system_time);
  ExSystemTimeToLocalTime(&system_time, &local_time);
  RtlTimeToTimeFields(&local_time, time);
  return STATUS_SUCCESS;
}

NTSTATUS windows::kernal::Time::GetCurrentTimeString(WCHAR buffer[24]) {
  // YYYY/MM/DD hh:mm:ss xxx, length = 23 + 1('\0')
#define REQUIRED_TIME_BUFFER_SIZE (sizeof(WCHAR) * 24)  
  WCHAR format[] = L"%04d/%02d/%02d %02d:%02d:%02d %3d";
  NTSTATUS status;
  TIME_FIELDS time_fields;

  status = GetCurrentTimeFields(&time_fields);
  if (NT_SUCCESS(status)) {
    return RtlStringCbPrintfW(buffer, REQUIRED_TIME_BUFFER_SIZE, format, 
      time_fields.Year, time_fields.Month, time_fields.Day, 
      time_fields.Hour, time_fields.Minute, time_fields.Second, time_fields.Milliseconds);
  }
  return status;
}
